package com.itic.appbase.applications.sys.region.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.itic.appbase.applications.sys.region.persistence.dao.RegionDao;
import com.itic.appbase.applications.sys.region.persistence.model.Region;
import com.itic.appbase.applications.sys.region.persistence.model.RegionExample;
import com.itic.appbase.applications.sys.region.persistence.model.RegionExample.Criteria;
import com.itic.appbase.applications.sys.region.persistence.model.RegionStat;
import com.itic.appbase.framework.common.constants.DBConst;
import com.itic.appbase.framework.common.persistence.model.DynamicParams;
import com.itic.appbase.framework.common.service.CrudService;
import com.itic.appbase.framework.utils.PropertyGetter;
import com.itic.appbase.framework.utils.StringHelper;

@Service
@Transactional
public class RegionService
        extends CrudService<RegionDao, Region, RegionExample> {
    @Autowired
    private RegionDao regionDao;

    /**
     * 行政区划树
     * 
     * @param region
     * @return
     */
    public List<Region> regionTree(Region region) {
    	String isMysql = PropertyGetter.getString("app.db.type");
    	if( StringHelper.isNotBlank(isMysql)&&"mysql".equals(isMysql) ){
    		RegionExample example = new RegionExample();
    		Criteria criteria = example.createCriteria().andIsEffectiveEqualTo(DBConst.TRUE);
    		if(StringHelper.isNotBlank(region.getId())){
        		criteria.andPIdEqualTo(region.getId());
    		}
    		return regionDao.selectByExample(example);
    	}else{
    		return regionDao.selectRegionList(region);
    	}
        
    }

    /**
     * 级联删除
     * 
     * @param id
     * @return
     */
    public String deleteTree(String id) {
        DynamicParams params = new DynamicParams();
        params.setParamster1(id);// id
        params.setParamster2(DBConst.TRUE);// dbconst.istrue
        params.setParamster3(DBConst.FALSE);// dbconst/isfalse
        int code = 0;
        String isMysql = PropertyGetter.getString("app.db.type");
    	if( StringHelper.isNotBlank(isMysql)&&"mysql".equals(isMysql) ){
    		Region one = regionDao.selectByPrimaryKey(id);
    		if( null != one ){
    			code += regionDao.deleteByPrimaryKey(id);
    			RegionExample example = new RegionExample();
    			Criteria criteria = example.createCriteria();
    			criteria.andPIdEqualTo(id);
    			code += regionDao.deleteByExample(example);
    		}
    	}else{
    	    code = regionDao.deleteTree(params);
    	}
        return 0 < code ? SUCCESS : FAILED;
    }

    /**
     * 按行政区划类别统计数量
     * 
     * @return List<RegionStat>
     */
    public List<RegionStat> statByType() {
        Region region = new Region();
        region.setIsEffective(DBConst.TRUE);
        return regionDao.statByType(region);
    }
    
    
    /** 
     * 根据code值获取Region
     * @param code
     * @return
     * @return Region 
    */
    public Region getRegionByCode(String code) {
        return regionDao.getRegionByCode(code);
    }
    
    
    /**
     * 基础数据库行政区划、地区人口查询行政区划列表
     * 
     * @param region
     * @return
     */
    public List<Region> getRegionTree(Region region) {
        return regionDao.getRegionList(region);
    }
    
    public List<Region> selectRegionTree(Region region) {
     // id不为null时，以id查询，pId置空
        if (null != region && StringHelper.isNotBlank(region.getId())) {
            region.setpId(null);
        }
        return regionDao.getRegionTree(region);
    }
}